package com.example.oauth2demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * description: SecurityConfig <br>
 * date: 2021/5/21 23:36 <br>
 * author: xiehui <br>
 * version: 1.0 <br>
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    // 授权管理器
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                // authorizeRequests 设置需要认证的请求
                .authorizeRequests()
                // antMatchers 匹配请求路径，permitAll直接放行
                .antMatchers("/oauth/**", "/login/**", "/logout/**").permitAll()
                // anyRequest剩下的请求，全部authenticated需要认证
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll();
    }
}
